(function (w) {
  var token = Cookies.get('token')
  w.progress = {
    init: function (CONTRACTID) {
      this.getAccountMes(CONTRACTID)
      this.exportExcel(CONTRACTID)
      var _this = this;
      // $('.contract-tab button').unbind().on('click', function () {
      //   _this.getAccountMes(CONTRACTID)
      // })
    },
    //获取账单信息
    getAccountMes: function (CONTRACTID) {
      var self = this
      hlhttp.get({
        headers: {token: token},
        url: 'contracts/bills',
        data: {contract_id: CONTRACTID},
        success: function (data) {
          if (data.length === 0) {
            $('head').append('<style>.js-progress .js-progress-content::before{ border:0; }</style>');
            $('.content-list-contract').html('')
            $('.js-progress-content').css('left', '0')
            $('.progress-bar').css('width', '0')
            $('.js-progress-content .received').html('0.00')
            $('.js-progress-content .receivable').html('0.00')
            $('.js-progress-content .proportion').html('0.00')
            $('.general').addClass('hidden')
            return
          } else {
            $('.general').removeClass('hidden')
          }
          self.dom.contractBill(data, CONTRACTID)
        }
      })
    },

    dom: {
      contractBill: function (data, id) {
        var receivedtotal = 0,    // 已收总金额
          receivedtotals = 0,   // 已收总金额 + 减免总金额
          receivabletotal = 0,  // 应收总金额
          proportion = 0,  // 账单百分比
          general = 0,   // 减免总金额
          beginDate,     // 开始日期
          endDate,       // 结束日期
          paymentDate,  // 应收日期
          paymentDeadline, // 截止日期
          str = '';

        for (var i = 0; i < data.length; i++) {

          if (data[i].paymentDate) {
            paymentDate = hlDateUtil.formatUTCDate(data[i].paymentDate, 'yyyy-MM-dd')
          } else {
            paymentDate = '--'
          }
          if (data[i].paymentDeadline) {
            paymentDeadline = hlDateUtil.formatUTCDate(data[i].paymentDeadline, 'yyyy-MM-dd')
          } else {
            paymentDeadline = '--'
          }
          if (data[i].beginDate) {
            beginDate = hlDateUtil.formatUTCDate(data[i].beginDate, 'yyyy-MM-dd')
          } else {
            beginDate = '--'
          }
          if (data[i].endDate) {
            endDate = hlDateUtil.formatUTCDate(data[i].endDate, 'yyyy-MM-dd')
          } else {
            endDate = '--'
          }


          str += '<div class="dy-flex border-t">'
            + '<div class="dy-fx-3 line-height-45 ellipsis-1"><a href="#/billDetail?ddtab=true&id='+data[i].id+'&aid='+id+'" target="_blank">' + data[i].no + '</a></div>'
            + '<div class="dy-fx-4 line-height-45 ellipsis-1">' + beginDate + ' 至 ' + endDate + '</div>'
            + '<div class="dy-fx-2 line-height-45 ellipsis-1">' + paymentDeadline + '</div>'
            + '<div class="dy-fx-2 line-height-45 ellipsis-1">' + hlNumberUtil.formatNum(data[i].amountReceive) + '</div>'
            + '<div class="dy-fx-2 line-height-45 ellipsis-1">' + paymentDate + '</div>'
            + '<div class="dy-fx-2 line-height-45 ellipsis-1">' + hlNumberUtil.formatNum(data[i].amountPayed) + '</div>'
            + '<div class="dy-fx-1 line-height-45 ellipsis-1">' + paymentStateCovert(data[i].paymentState) + '</div>'
            + '</div>';

          receivedtotal += parseFloat(data[i].amountPayed);
          receivedtotals += (parseFloat(data[i].amountPayed))
          receivabletotal += (parseFloat(data[i].amountReceive))
          general += parseFloat(data[i].derateAmount) + parseFloat(data[i].derateLateFee);
        }
        $('.content-list-contract').html(str)
        if (general > 0) {
          $('.js-progress-content .general').html('共减免本金滞纳金：' + general + '元')
        } else {
          $('.js-progress-content .general').addClass('hidden')
        }

        if (Number(parseFloat(receivedtotals / receivabletotal) * 100 - 2) == -2) {
          $('head').append('<style>.js-progress .js-progress-content::before{ border:0; }</style>');
        } else {
          setTimeout(function () {
            var progress = (Number(hlNumberUtil.formatThousandsNum($('.received').text()))) / Number(hlNumberUtil.formatThousandsNum($('.receivable').text())) * 100 - 2;
            if (Number(progress) >= 98) {
              progress = '94.00'
            }
            if (Number(progress) < 1) {
              progress = '1'
            }
            $('head').append('<style>.js-progress .js-progress-content::before{ left:' + progress + '%; border:6px solid transparent; border-top-color:#5e77ad;  }</style>');
          }, 200);
        }

        $('.js-progress-content .received').html(hlNumberUtil.formatNum(receivedtotal.toFixed(2)))
        $('.js-progress-content .receivable').html(hlNumberUtil.formatNum(Number(receivabletotal).toFixed(2)))

        if (Number(receivabletotal).toFixed(2) === '0.00') {
          $('.js-progress-content .proportion').text('0.00')
        } else {
          var ld = (Number(hlNumberUtil.formatThousandsNum($('.received').text())) / Number(hlNumberUtil.formatThousandsNum($('.receivable').text())) * 100).toFixed(2)
          $('.js-progress-content .proportion').html(ld)
        }


        $('.js-progress-content .general').css({
          left: -$('.js-progress-content .general').outerWidth() + '100'
        })

        var maxWidth;
        if (Number(parseFloat(receivedtotals / receivabletotal) * 100).toFixed(2) > 100) {
          maxWidth = 100
        } else {
          maxWidth = Number(parseFloat(receivedtotals / receivabletotal) * 100).toFixed(2)
        }

        $('.js-progress .progress-bar').css({
          width: maxWidth + '%'
        })

        var loaing = Number(parseFloat(receivedtotals / receivabletotal) * (957 - parseFloat($('.js-progress-content').outerWidth()))).toFixed(2);
        if (loaing < 0) {
          loaing = 0;
        } else {
          loaing = Number(parseFloat(receivedtotals / receivabletotal) * (957 - parseFloat($('.js-progress-content').outerWidth()))).toFixed(2)
        }

        $('.js-progress-content').stop(true).animate({
          left: loaing
        }, 550)
      }
    },

    //导出
    exportExcel: function (CONTRACTID) {
      $('.js-execl-export-btn').unbind().on('click', function () {
        $.ajax({
          url: exclUrl+'resource/bill_excel',
          type: 'get',
          headers: {
            token: Cookies.get("token"),
          },
          data: {
            contract_id: CONTRACTID
          },
          success: function (res) {
            var a = document.createElement('a');
            a.href = res.excel_url;
            $('body').append($(a))
            a.click();
            setTimeout(function() {
              $(a).remove()
            }, 100);
          }
        })

      })
    }
  }

  function paymentStateCovert(state) {
    var result;
    switch (state) {
      case 'WP':
        result = '未收'
        break;
      case 'APE':
        result = '已收'
        break;
      case 'PPE':
        result = '部分收款'
        break;
      case '未收':
        result = 'WP'
        break;
      case '已收':
        result = 'APE'
        break;
      case '部分收款':
        result = 'PPE'
        break;
      default:
        result = ''
    }
    return result;
  }
}(window))
